﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CatsAndMice.Models;
using System.Collections.ObjectModel;
using System.Windows;

namespace CatsAndMice.ViewModels
{
    class GameViewModel : DependencyObject
    {

        /// <summary>
        /// The current game model
        /// </summary>
        public Game CurrentGame { get; set; }

        public GameViewModel()
        {
            // TODO 
            CatsAndMice.Models.Mouse mouseA = new CatsAndMice.Models.Mouse(0, "Player 1");
            CatsAndMice.Models.Mouse mouseB = new CatsAndMice.Models.Mouse(1, "Player 2");

            List<CatsAndMice.Models.Mouse> players = new List<CatsAndMice.Models.Mouse>();
            players.Add(mouseA);
            players.Add(mouseB);

            CurrentGame = new Game(0, players, 10, Difficulty.EASY);
            CurrentGame.CurrentPlayer = mouseA;
            
        }

       
        private RelayCommand _MoveMouseCommand;

        public ICommand MoveMouseCommand
        {
            get
            {
                if (_MoveMouseCommand == null)
                {
                    return _MoveMouseCommand = new RelayCommand(MoveMouse, IsMovePlayerAllowed);
                }
                return _MoveMouseCommand;
            }
        }


        private RelayCommand _RollDiceCommand;

        public ICommand RollDiceCommand
        {
            get
            {
                if (_RollDiceCommand == null)
                {
                    _RollDiceCommand = new RelayCommand(RollDice, IsDiceRollAllowed);
                }
                return _RollDiceCommand;
            }
        }

        /// <summary>
        /// Displays/Highlights the target field for the current player and waits until the current player has moved to that specific field
        /// </summary>
        /// <param name="param"></param>
        public void MoveMouse(object param)
        {
            //Move the mouse
            //just switch to the next player and allow dice rolling again

            //in real live it must be checked if the new field is the target field
            //for now we can just set the current field to the target field and then 
            CurrentGame.CurrentPlayer.CurrentPosition = CurrentGame.CurrentPlayer.TargetPosition;
            CurrentGame.CurrentPlayer.CurrentState = State.MOVED;

            CurrentGame.CurrentPlayer = CurrentGame.NextPlayer;
            CurrentGame.CurrentPlayer.CurrentState = State.START;
        }

        /// <summary>
        /// Rolls the dice so it shows a new value between 1 and 6
        /// </summary>
        /// <param name="param"></param>
        public void RollDice(object param)
        {
            CurrentGame.Dice.Roll();
#if DEBUG
            Console.WriteLine("Dice is rolling");
            Console.WriteLine("Value: " + CurrentGame.Dice.Value);
#endif
            
            CurrentGame.CurrentPlayer.MayMove = CurrentGame.MayCurrentPlayerMove();
#if DEBUG
            Console.WriteLine("May player " + CurrentGame.CurrentPlayer + " move? " + CurrentGame.CurrentPlayer.MayMove);
#endif
            
            if (CurrentGame.CurrentPlayer.MayMove)
            {
                //this may only occur if the current player can be moved!
                CurrentGame.CurrentPlayer.TargetPosition = CurrentGame.CalculateTargetPositionForCurrentPlayer();
                //the target field should be highlighted here
                //and via a simple gui click the mouseMoved command must be raised
                CurrentGame.CurrentPlayer.CurrentState = State.ROLLED;
            }
            else
            {
                //if current player may not move just switch to the new player, no setting of any player pawn is needed
                CurrentGame.CurrentPlayer = CurrentGame.NextPlayer;
                CurrentGame.CurrentPlayer.CurrentState = State.START;
            }
        }

        /// <summary>
        /// Returns if the current player is allowed to roll the dice.
        /// If the current player has already rolled the dice another roll is not allowed until the player has moved its pawn to the highlighted target field.
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool IsDiceRollAllowed(object param)
        {
            //define when exactly dice rolling is allowed
            return CurrentGame.CurrentPlayer.CurrentState == State.START;
        }

        public bool IsMovePlayerAllowed(object param)
        {
            return CurrentGame.CurrentPlayer.MayMove && CurrentGame.CurrentPlayer.CurrentState == State.ROLLED;
        }

        private int CalculateTargetPosition()
        {
            return 0;
        }
    }    
}
